<template>
  <div>
    <router-link tag="div" to="/" class="header-back" v-show="showHeader">
      <span class="iconfont back-icon">&#xe65b;</span>
    </router-link>
    <div class="header-info" v-show="!showHeader" :style="opacityStyle">
      <router-link to="/">
        <span class="iconfont back-fixed-icon">&#xe65b;</span>
      </router-link>
      景点详情
    </div>
  </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  data () {
    return {
      showHeader: true,
      opacityStyle: {
        opacity: 0
      }
    }
  },
  mounted () {
    window.addEventListener('scroll', this.handleScroll)
  },
  unmounted () {
    // 对全局事件的解绑
    window.removeEventListener('scroll', this.handleScroll)
  },
  methods: {
    handleScroll () {
      const top = document.documentElement.scrollTop
      if (top > 60) {
        let opacity = top / 140
        opacity = opacity > 1 ? 1 : opacity
        this.opacityStyle = { opacity }
        this.showHeader = false
      } else {
        this.showHeader = true
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.header-back {
  position: absolute;
  left: 0.2rem;
  top: 0.2rem;
  width: 0.8rem;
  height: 0.8rem;
  line-height: 0.8rem;
  border-radius: 0.4rem;
  text-align: center;
  background: rgba(0, 0, 0, 0.8);
  .back-icon {
    color: #fff;
    font-size: 0.4rem;
  }
}
.header-info{
  position: fixed;
  z-index: 2;
  top: 0;
  right: 0;
  left: 0;
  height: .86rem;
  line-height: .86rem;
  color: #fff;
  font-size: .32rem;
  text-align: center;
  background: #00bcd4;
  .back-fixed-icon{
    position: absolute;
    top: 0;
    left: 0;
    width: .64rem;
    font-size: .4rem;
    text-align: center;
    color: #fff;
  }
}
</style>
